package com.baseinfinityrun;

import java.util.List;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import com.BGS.Background;
import com.BGS.ChildrenBG;
import com.BGS.PresentBG;
import com.Era.Child;
import com.Era.Era;
import com.Era.Present;
import com.Players.ChildPlayer;
import com.Players.Player;
import com.Players.PresentPlayer;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.gl.Camera2D;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.impl.GLGraphics;
import com.badlogic.androidgames.framework.math.Vector2;

public class GameScreen extends Screen
{
	enum ERA
	{
		Present,
		Young,
		Child,
		Ancient,
		Adult
	}
	
	public static Vector2 screenSize;
	public static float FRUSTUM_WIDTH = 4.8f;
	public static float FRUSTUM_HEIGHT = 3.2f;
	
	float animTime, a;
	int numberOfBG = 1;
	
	SpriteBatcher batcher;
	Camera2D camera;
	GLGraphics glGraphics;
	Player player;
	Era actualEra;
	Background bg;
	Object EraState = new Object();
	boolean changeEra = false;
	Game myGame;

	public GameScreen(Game game) 
	{
		super(game);
		glGraphics = ((GLGame)game).getGLGraphics();
		camera = new Camera2D(glGraphics,FRUSTUM_WIDTH,FRUSTUM_HEIGHT);		
		batcher = new SpriteBatcher(glGraphics, 10);
		
		screenSize = new Vector2(glGraphics.getWidth(),glGraphics.getHeight());
		
		myGame = game;
		
		a = 1;
		EraState = ERA.Present;
		SetEra();
	}

	@Override
	public void update(float deltaTime) 
	{		
		if(!changeEra)
		{
			List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
			actualEra.ReceiveTouch(touchEvents);
			actualEra.Update(deltaTime);
			if(actualEra.CollisionObstacles().equalsIgnoreCase("obstacle") || actualEra.CollisionObstacles().equalsIgnoreCase("portal"))
			{
				changeEra = true;
				ChangeEra(actualEra.CollisionObstacles());
			}
		}
		
		else
		{
			a -= deltaTime * 1.3f;
			if(a < 0)
			{
				SetEra();
				a = 1;
				changeEra = false;
			}
		}
	}

	@Override
	public void present(float deltaTime)
	{
		 GL10 gl = glGraphics.getGL();
	     gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	     actualEra.ChangeCamera(camera);
	     
	     gl.glEnable(GL10.GL_BLEND);
	     gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
	     gl.glEnable(GL10.GL_TEXTURE_2D);
	     gl.glColor4f(1, 1, 1, a);
	     
	     actualEra.Draw(batcher); 
	     
	     gl.glDisable(GL10.GL_TEXTURE_2D);
	     gl.glDisable(GL10.GL_BLEND);
	 }

	@Override
	public void pause() 
	{
		
	}

	@Override
	public void resume()
	{
		//Assets.Load((GLGame)game);
	}

	@Override
	public void dispose()
	{
		
	}
	
	private void ChangeEra(String type)
	{
		if(EraState != ERA.Present)
		{
			EraState = ERA.Present;
		}
		
		else if(type.equalsIgnoreCase("portal"))
		{
			Random r = new Random();
			int era = r.nextInt(25)%1;
			
			switch(era)
			{
				case 0:
					EraState = ERA.Child;
					break;
					
				case 1:
					EraState = ERA.Young;
					break;
					
				case 2:
					EraState = ERA.Adult;
					break;
				
				case 3:
					EraState = ERA.Ancient;
					break;
					
				default:
					EraState = ERA.Present;
					break;
			}
		}
		changeEra = true;
	}
	
	private void SetEra()
	{
		synchronized(EraState)
		{
			if(EraState == ERA.Present)
			{
				player = new PresentPlayer(1, 2, 0.99f, 1.32f);		
				bg = new PresentBG(FRUSTUM_WIDTH - FRUSTUM_WIDTH * 1.4f, FRUSTUM_HEIGHT/2, FRUSTUM_WIDTH * 1.2f, FRUSTUM_HEIGHT );
				actualEra = new Present((PresentPlayer)player, (PresentBG)bg, this.myGame);
			}
			
			if(EraState == ERA.Child)
			{
				player = new ChildPlayer(1, 2, 0.99f, 1.32f);		
				bg = new ChildrenBG(FRUSTUM_WIDTH - FRUSTUM_WIDTH * 1.4f, FRUSTUM_HEIGHT/2, FRUSTUM_WIDTH * 1.2f, FRUSTUM_HEIGHT );
				actualEra = new Child((ChildPlayer)player, (ChildrenBG)bg, this.myGame);
			}
		}
	}
}
